package com.zjkj.mp.controller.customerManage;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.customer.CustomerManagerService;
import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.utils.security.SpringSecurityUtils;
import com.zjkj.mp.vo.customer.ContactPerson;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 联系人列表控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class ConcatePersonListController {

	@Resource(name = "userServiceImpl")
	private UserService userService;
	@Autowired
	private CustomerManagerService customerManagerService;
	private static Log log = LogFactory.getLog(ConcatePersonListController.class);
	/******************************* 联系人列表 ***********************************/
	
	/**
	 * 显示通讯簿列表
	 */
	@RequestMapping("/queryCustomerList.do")
	public String customerList(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = new HashMap<String, String>();
		String userName = request.getParameter("username");
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		int userId = ((MpUser)SpringSecurityUtils.getCurrentUser()).getUserId();
		String currentPage = request.getParameter("currrentPage");
		String pageSize = "10";
		int nextPage = 0;
		int proPage = 0;
		if("".equals(currentPage) || currentPage == null){
			currentPage = "1";
		}
		map.put("pageSize", pageSize);
		map.put("pageNo", currentPage);
		map.put("userId", userId+"");
		try {
	    PaginatedResult obj =	customerManagerService.findConactPersonList(map);
		List<ContactPerson> list = (List<ContactPerson>) obj.getResult();
		nextPage = Integer.parseInt(currentPage)+1;
		proPage = Integer.parseInt(currentPage)-1;
		if(nextPage >= obj.getPageCount()){
			nextPage = obj.getPageCount();
		}
		if(proPage <= 0){
			proPage = 1;
		}
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("resultList", list);
		request.setAttribute("nextPage",nextPage);
		request.setAttribute("proPage", proPage);
		request.setAttribute("pageCount", obj.getPageCount());
		} catch (Exception e) {
			log.error("获取联系人列表异常",e);
		}
		return "customer/customerList";
		
	}
	/**
	 * 显示编辑联系人信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/editNewcustomer.do")
	public String editNewcustomer(HttpServletRequest request,HttpServletResponse response){
		String concatId = request.getParameter("customerId");//获取联系人id
		String userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId()+"";//创建人id
		String page = request.getParameter("page");
		Map<String, String> map = new HashMap<String, String>();
		map.put("concatId", concatId);
		map.put("userId", userId);
		try {
			ContactPerson person = customerManagerService.findConactPersonByParm(map);
			request.setAttribute("person", person);
			request.setAttribute("page", page);
		} catch (Exception e) {
			log.error("编辑联系人异常",e);
		}
		return "customer/editCustomer";
	}
	
	
	/**
	 * 删除联系人操作
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/deleteNewcustomer.do")
	public String deleteNewcustomer(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		Map<String, String> map = new HashMap<String, String>();
		String userid = request.getParameter("customerId");
		String message = null;
		String error = null;
		try {
			map.put("userid", userid);
			customerManagerService.deleteConcatePerson(map);
			message = "删除联系人信息成功";
		} catch (Exception e) {
			error = "删除联系人信息失败";
			log.error("删除联系人信息异常",e);
		}
		request.setAttribute("message", message);
		request.setAttribute("error", error);
		RequestDispatcher rd = request.getRequestDispatcher("/queryCustomerList.do");
		rd.forward(request, response);
		return null;
	}

	/**
	 * 修改联系人信息的操作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/doEditNewcustomer.do")
	public String doEditNewcustomer(HttpServletRequest request,HttpServletResponse response){
		try {
			String page = request.getParameter("page");
			String concatId = request.getParameter("concatId");//获取联系人id
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();//创建人id
			ContactPerson person = new ContactPerson();
			person.setUserid(concatId);
			person.setCreated(userId);
			String value = "";
			value = request.getParameter("name");
			person.setName(value);
			value = request.getParameter("movePhoneNumber");
			person.setMovePhoneNumber(value);
			value = request.getParameter("phoneNumber");
			person.setPhoneNumber(value);
			value = request.getParameter("eMail");
			person.seteMail(value);
			value = request.getParameter("birthday");
			person.setBirthday(value);
			value = request.getParameter("company");
			person.setCompany(value);
			value = request.getParameter("address");
			person.setAddress(value);
			value = request.getParameter("sex");
			person.setSex(value);
			value = request.getParameter("remark");
			person.setRemark(value);
			
			customerManagerService.updateConcatePerson(person);
			request.setAttribute("currrentPage", page);
			RequestDispatcher rd = request.getRequestDispatcher("/queryCustomerList.do");
			rd.forward(request, response);
		} catch (Exception e) {
			log.error("修改联系人信息异常", e);
		}
		return null;
	}
	
	/**
	 * 这是列表中查询的功能
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryCustomerListByParam.do")
	public String queryCustomerListByParam(HttpServletRequest request,HttpServletResponse response){
		
		try {
			Map<String,String> map = new HashMap<String, String>();
			String userName = request.getParameter("username");//获取用户名，这里有一个默认的操作，就是当前用户下面只可以查看自己的用户名，管理员也不能查看别人的
			int createdId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();//获取当前用户的id
			String name = request.getParameter("name");//联系人姓名
			String company = request.getParameter("company");//联系人单位
			String currentPage = request.getParameter("currrentPage");//当前页数
			map.put("createdId", createdId+"");
			map.put("name",name);
			map.put("company", company);
			map.put("pageSize", "10");
			String pageSize = "10";
			int nextPage = 0;
			int proPage = 0;
			if("".equals(currentPage) || currentPage == null){
				currentPage = "1";
			}
			map.put("pageNo", currentPage);
			nextPage = Integer.parseInt(currentPage)+1;
			proPage = Integer.parseInt(currentPage)-1;
			PaginatedResult obj = customerManagerService.findQueryConcatePerson(map);
			List<ContactPerson> list = (List<ContactPerson>) obj.getResult();
			if(nextPage >= obj.getPageCount()){
				nextPage = obj.getPageCount();
			}
			if(proPage <= 0){
				proPage = 1;
			}
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("resultList", list);
			request.setAttribute("nextPage",nextPage);
			request.setAttribute("proPage", proPage);
			request.setAttribute("pageCount", obj.getPageCount());
			request.setAttribute("name", name);
			request.setAttribute("company", company);
		 } catch (Exception e) {
			log.error("获取查询联系人结果异常", e);
		}
		return "customer/showQueryList";
		
	}
}
